Imports System.Xml
Imports System.text
Imports System.IO
Imports System.Web

Public Class Database
    Inherits ObjectBase

    Private mlDBID As Long
    Private mlServerID As Long
    Private msName As String
    Private msDesc As String
    Private msRevDate As String = Now
    Private mbIsWorkingCopy As Boolean = False

    Private mbIsActive As Boolean = True

    Private msEncryptedConnectionString As String
    Private msDecryptedConnectionString As String

    Dim moLicense As New SQLAutoDocLicense

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub New(ByVal lID As Long)
        MyBase.New(lID)
    End Sub

    Public Sub New(ByVal lID As Long, ByVal lRevID As Long)
        MyBase.New(lID, lRevID)
    End Sub

#Region "Properties"
    Public Property ServerID() As Long
        Get
            Return mlServerID
        End Get
        Set(ByVal value As Long)
            mlServerID = value
        End Set
    End Property

    Public Property Name() As String
        Get
            Return msName
        End Get
        Set(ByVal value As String)
            msName = value
        End Set
    End Property

    Public Property Desc() As String
        Get
            Return msDesc
        End Get
        Set(ByVal value As String)
            msDesc = value
        End Set
    End Property

    Public Property RevDate() As String
        Get
            Return msRevDate
        End Get
        Set(ByVal value As String)
            msRevDate = value
        End Set
    End Property

    Public Property DBID() As Long
        Get
            Return mlDBID
        End Get
        Set(ByVal value As Long)
            mlDBID = value
        End Set
    End Property

    Public Property IsWorkingCopy() As Boolean
        Get
            Return mbIsWorkingCopy
        End Get
        Set(ByVal value As Boolean)
            mbIsWorkingCopy = value
        End Set
    End Property

    Public Property IsActive() As Boolean
        Get
            Return mbIsActive
        End Get
        Set(ByVal value As Boolean)
            mbIsActive = value
        End Set
    End Property

    Public WriteOnly Property ConnectionString() As String
        Set(ByVal value As String)
            EncryptConnectionString(value)
        End Set
    End Property

    Friend Property EncryptedConnectionString() As String
        Get
            Return msEncryptedConnectionString
        End Get
        Set(ByVal value As String)
            msEncryptedConnectionString = value
        End Set
    End Property

    Friend Property DecryptedConnectionString() As String
        Get
            If msDecryptedConnectionString > "" Then
                Return msDecryptedConnectionString
            Else
                msDecryptedConnectionString = Decrypt(msEncryptedConnectionString)
                Return msDecryptedConnectionString
            End If
        End Get
        Set(ByVal value As String)
            msDecryptedConnectionString = value
            msEncryptedConnectionString = value
        End Set
    End Property

    Friend Sub EncryptConnectionString(ByVal sConnectionString As String)
        msEncryptedConnectionString = Encrypt(sConnectionString)
        msDecryptedConnectionString = sConnectionString
    End Sub
#End Region

#Region "database"
    Public Sub Load()
        Dim oObject As New DBDatabase

        If mlRevID > -1 Then
            oObject.LoadRev(Me, mlRevID)
        Else
            oObject.Load(Me)
        End If
    End Sub

    Public Sub LoadWorkingRev()
        Dim oObject As New DBDatabase

        oObject.LoadWorkingRev(Me)
    End Sub

    Public Sub Save()
        Dim oObject As New DBDatabase

        If mbObjectExists = True Then
            oObject.Update(Me)
        Else
            Dim oNextID As New DBNextIDValue("Database")
            Dim lNextID As Long = oNextID.NextValue

            Me.ID = lNextID
            Me.RevID = 0
            Me.CreateDate = Now
            oObject.Add(Me)
            mbObjectExists = True
        End If
    End Sub

    Public Sub Rev()
        Dim oObject As New DBDatabase

        '---Previous working copy is now permanent copy
        Me.IsWorkingCopy = False
        Me.RevDate = Now
        Me.Save()

        '---Add new working copy
        mlRevID = mlRevID + 1
        Me.CreateDate = Now
        Me.IsWorkingCopy = True

        oObject.Add(Me)

        mbObjectExists = True
    End Sub

    Public Sub Delete()
        Dim oObject As New DBDatabase

        oObject.Delete(Me)
        mbObjectExists = False
    End Sub
#End Region

#Region "Encryption"
    Private Function Decrypt(ByVal sValue As String) As String
        Return moLicense.Decrypt(sValue)
    End Function

    Private Function Encrypt(ByVal sValue As String) As String
        Return moLicense.Encrypt(sValue)
    End Function
#End Region

    Public Function ToXML() As String
        Dim oServer As New SQLAutoDocLib.Server(Me.ServerID)
        oServer.Load()

        Dim oDoc As New XmlDocument

        Dim oMS As New MemoryStream
        Dim oXMLW As New XmlTextWriter(oMS, Encoding.UTF8)
        Dim oOS As New StreamReader(oMS)

        oXMLW.WriteStartDocument()
        oXMLW.WriteProcessingInstruction("xml-stylesheet", "type=" & Q & "text/xsl" & Q & " href=" & Q & "http://" & HttpContext.Current.Session("BASEDIR") & "/sad/t1.xsl" & Q)

        oXMLW.WriteStartElement("database")

        oXMLW.WriteStartAttribute("name")
        oXMLW.WriteValue(Me.Name)
        oXMLW.WriteEndAttribute()

        oXMLW.WriteStartAttribute("ServerID")
        oXMLW.WriteValue(Me.ServerID)
        oXMLW.WriteEndAttribute()

        oXMLW.WriteStartAttribute("ServerName")
        oXMLW.WriteValue(oServer.Name)
        oXMLW.WriteEndAttribute()

        oXMLW.WriteStartAttribute("desc")
        oXMLW.WriteValue(Me.Desc)
        oXMLW.WriteEndAttribute()

        oXMLW.WriteStartAttribute("id")
        oXMLW.WriteValue(Me.ID)
        oXMLW.WriteEndAttribute()

        oXMLW.WriteStartAttribute("rev")
        oXMLW.WriteValue(Me.RevID)
        oXMLW.WriteEndAttribute()

        oXMLW.WriteStartAttribute("createdate")
        oXMLW.WriteValue(Me.CreateDate)
        oXMLW.WriteEndAttribute()

        '---Tables
        oXMLW.WriteStartElement("dbtables")
        Dim oTableFactory As New Table_Factory
        For Each oTable As SQLAutoDocLib.Table In oTableFactory.GetAllTablesForDatabase(Me.ID, Me.RevID)
            oXMLW.WriteStartElement("table")

            With oTable
                oXMLW.WriteStartAttribute("name")
                oXMLW.WriteValue(.Name)
                oXMLW.WriteEndAttribute()

                oXMLW.WriteStartAttribute("owner")
                Dim oOwner As New Login(.OwnerID)
                oOwner.Load()
                oXMLW.WriteValue(oOwner.Login)
                oXMLW.WriteEndAttribute()

                oXMLW.WriteStartAttribute("createdate")
                oXMLW.WriteValue(.CreateDate)
                oXMLW.WriteEndAttribute()

                oXMLW.WriteStartAttribute("desc")
                oXMLW.WriteValue(.Desc)
                oXMLW.WriteEndAttribute()

                oXMLW.WriteStartAttribute("id")
                oXMLW.WriteValue(.ID)
                oXMLW.WriteEndAttribute()

                oXMLW.WriteStartAttribute("rev")
                oXMLW.WriteValue(.RevID)
                oXMLW.WriteEndAttribute()

                oXMLW.WriteStartElement("fields")
                Dim oFieldFactory As New Field_Factory
                For Each oField As SQLAutoDocLib.Field In oFieldFactory.GetAllFieldsForTable(.ID, .RevID)
                    oXMLW.WriteStartElement("field")
                    oDoc.LoadXml(oField.SerializeMe)
                    oXMLW.WriteRaw(oDoc.ChildNodes(1).InnerXml)
                    oXMLW.WriteEndElement()
                Next
                oXMLW.WriteEndElement() 'fields

                oXMLW.WriteStartElement("indexes")
                Dim oIndexFactory As New Index_Factory
                For Each oIndex As SQLAutoDocLib.Index In oIndexFactory.GetAllIndexesForTable(.ID, .RevID)
                    oXMLW.WriteStartElement("index")
                    oDoc.LoadXml(oIndex.SerializeMe)
                    oXMLW.WriteRaw(oDoc.ChildNodes(1).InnerXml)
                    oXMLW.WriteEndElement()
                Next
                oXMLW.WriteEndElement() 'Indexes
            End With

            oXMLW.WriteEndElement() 'table
        Next
        oXMLW.WriteEndElement() 'dbtables

        '---Views
        oXMLW.WriteStartElement("dbviews")
        Dim oViewFactory As New View_Factory
        For Each oView As SQLAutoDocLib.View In oViewFactory.GetAllViewsForDatabase(Me.ID, Me.RevID)
            oXMLW.WriteStartElement("view")

            With oView
                oXMLW.WriteStartAttribute("name")
                oXMLW.WriteValue(.Name)
                oXMLW.WriteEndAttribute()

                oXMLW.WriteStartAttribute("owner")
                Dim oOwner As New Login(.OwnerID)
                oOwner.Load()
                oXMLW.WriteValue(oOwner.Login)
                oXMLW.WriteEndAttribute()

                oXMLW.WriteStartAttribute("createdate")
                oXMLW.WriteValue(.CreateDate)
                oXMLW.WriteEndAttribute()

                oXMLW.WriteStartAttribute("desc")
                oXMLW.WriteValue(.Desc)
                oXMLW.WriteEndAttribute()

                oXMLW.WriteStartAttribute("id")
                oXMLW.WriteValue(.ID)
                oXMLW.WriteEndAttribute()

                oXMLW.WriteStartAttribute("rev")
                oXMLW.WriteValue(.RevID)
                oXMLW.WriteEndAttribute()

                Dim oTextFactory As New VText_Factory
                oXMLW.WriteStartElement("vtext")
                oXMLW.WriteCData(System.Web.HttpUtility.HtmlEncode(oTextFactory.GetText(.ID, .RevID)))
                oXMLW.WriteEndElement() 'vtext
            End With

            oXMLW.WriteEndElement() 'view
        Next
        oXMLW.WriteEndElement() 'dbviews

        '---Stored Procedures
        oXMLW.WriteStartElement("dbprocedures")
        Dim oSProcFactory As New SProc_Factory
        For Each oSProc As SQLAutoDocLib.SProc In oSProcFactory.GetAllSProcsForDatabase(Me.ID, Me.RevID)
            oXMLW.WriteStartElement("procedure")

            With oSProc
                oXMLW.WriteStartAttribute("name")
                oXMLW.WriteValue(.Name)
                oXMLW.WriteEndAttribute()

                oXMLW.WriteStartAttribute("owner")
                Dim oOwner As New Login(.OwnerID)
                oOwner.Load()
                oXMLW.WriteValue(oOwner.Login)
                oXMLW.WriteEndAttribute()

                oXMLW.WriteStartAttribute("createdate")
                oXMLW.WriteValue(.CreateDate)
                oXMLW.WriteEndAttribute()

                oXMLW.WriteStartAttribute("desc")
                oXMLW.WriteValue(.Desc)
                oXMLW.WriteEndAttribute()

                oXMLW.WriteStartAttribute("id")
                oXMLW.WriteValue(.ID)
                oXMLW.WriteEndAttribute()

                oXMLW.WriteStartAttribute("rev")
                oXMLW.WriteValue(.RevID)
                oXMLW.WriteEndAttribute()

                Dim oTextFactory As New SProcText_Factory
                oXMLW.WriteStartElement("ptext")
                oXMLW.WriteCData(System.Web.HttpUtility.HtmlEncode(oTextFactory.GetText(.ID, .RevID)))
                oXMLW.WriteEndElement() 'ptext
            End With

            oXMLW.WriteEndElement() 'procedure
        Next
        oXMLW.WriteEndElement() 'dbprocedures

        '---Stored Procedures
        oXMLW.WriteStartElement("dbfunctions")
        Dim oFunctionFactory As New Function_Factory
        For Each oSProc As SQLAutoDocLib.Function In oFunctionFactory.GetAllFunctionsForDatabase(Me.ID, Me.RevID)
            oXMLW.WriteStartElement("function")

            With oSProc
                oXMLW.WriteStartAttribute("name")
                oXMLW.WriteValue(.Name)
                oXMLW.WriteEndAttribute()

                oXMLW.WriteStartAttribute("owner")
                Dim oOwner As New Login(.OwnerID)
                oOwner.Load()
                oXMLW.WriteValue(oOwner.Login)
                oXMLW.WriteEndAttribute()

                oXMLW.WriteStartAttribute("createdate")
                oXMLW.WriteValue(.CreateDate)
                oXMLW.WriteEndAttribute()

                oXMLW.WriteStartAttribute("desc")
                oXMLW.WriteValue(.Desc)
                oXMLW.WriteEndAttribute()

                oXMLW.WriteStartAttribute("id")
                oXMLW.WriteValue(.ID)
                oXMLW.WriteEndAttribute()

                oXMLW.WriteStartAttribute("rev")
                oXMLW.WriteValue(.RevID)
                oXMLW.WriteEndAttribute()

                Dim oTextFactory As New SProcText_Factory
                oXMLW.WriteStartElement("ftext")
                oXMLW.WriteCData(System.Web.HttpUtility.HtmlEncode(oTextFactory.GetText(.ID, .RevID)))
                oXMLW.WriteEndElement() 'ftext
            End With

            oXMLW.WriteEndElement() 'function
        Next
        oXMLW.WriteEndElement() 'dbfunctions

        oXMLW.WriteEndElement() 'Database

        oXMLW.WriteEndDocument()    'Document

        oXMLW.Flush()

        Dim oEncoding As System.Text.UTF8Encoding = New System.Text.UTF8Encoding
        Dim sXML As String = oEncoding.GetString(oMS.GetBuffer)

        Dim iLastNULL As Integer = sXML.IndexOf(Chr(0)) - 1
        If iLastNULL = -1 Then iLastNULL = sXML.Length - 1

        Return sXML.Substring(1, iLastNULL)
    End Function
End Class
